#include "digitalclockwidget.h"
#include <QPainter>

DigitalClockWidget::DigitalClockWidget(QWidget* parent)
	: QWidget(parent)
	, m_showSeconds(true)
	, m_darkTheme(false)
	, m_user24Hour(false)
	, m_timeString()
	, m_dateString()
	, m_timeFont("Courier", 36, QFont::Bold)
	, m_dateFont("Times New Roman", 14)
{
	setMinimumSize(300, 150);
}

void DigitalClockWidget::setTime(const QDateTime& time)
{
	m_time = time;
	updateDisplay();
	update();
}

void DigitalClockWidget::setShowSeconds(bool show)
{
	m_showSeconds = show;
	updateDisplay();
	update();
}

void DigitalClockWidget::setDarkTheme(bool dark)
{
	m_darkTheme = dark;
	update();
}

void DigitalClockWidget::setUse24Hour(bool use24)
{
	m_user24Hour = use24;
	updateDisplay();
	update();
}

void DigitalClockWidget::paintEvent(QPaintEvent* event)
{
	Q_UNUSED(event);

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	
	QColor backgroundColor = m_darkTheme ? QColor(40, 40, 40) : QColor(240, 240, 240);
	QColor textColor = m_darkTheme ? QColor(0, 255, 0) : Qt::black;
	QColor dateColor = m_darkTheme ? QColor(200, 200, 200) : QColor(100, 100, 100);

	painter.fillRect(rect(), backgroundColor);

	painter.setFont(m_timeFont);
	painter.setPen(textColor);

	QRect timeRect = rect().adjusted(20, 20, -20, -60);
	painter.drawText(timeRect, Qt::AlignCenter, m_timeString);

	painter.setFont(m_dateFont);
	painter.setPen(dateColor);

	QRect dateRect = rect().adjusted(20, rect().height() - 50, -20, -10);
	painter.drawText(dateRect, Qt::AlignCenter, m_dateString);

	painter.setPen(QPen(m_darkTheme ? QColor(100, 100, 100) : QColor(200, 200, 200), 2));
	painter.drawRect(rect().adjusted(1, 1, -1, -1));
}

void DigitalClockWidget::updateDisplay()
{
	if (!m_time.isValid())
		return;
	QTime time = m_time.time();

	if (m_user24Hour) 
		m_timeString = time.toString(m_showSeconds ? "hh:mm:ss" : "hh:mm");
	else 
		m_timeString = time.toString(m_showSeconds ? "h:mm:ss AP" : "h:mm AP");
	m_dateString = m_time.toString("yyyy/MM/dd dddd");
}
